import moment from "moment"
import { ElMessage } from 'element-plus'

class UtilsServiceTemp {
    /**获取前monthNum个月的月份 */
    getMonthes(monthNum: number, currentYear: number, currenMonth: number) {
        let datelist = [
            {
                year: currentYear,
                month: currenMonth
            }
        ];
        if (monthNum < 0) {
            for (let i = 1; i < -monthNum; i++) {
                let last = datelist[datelist.length - 1];
                let date = {} as { year: number, month: number };
                //上一年12月
                if (last.month - 1 <= 0) {
                    date.month = 12
                    date.year = last.year - 1
                }
                else {
                    date.month = last.month - 1
                    date.year = last.year
                }
                datelist.push(date)
            }
        }
        else {
            for (let i = 1; i < monthNum; i++) {
                let last = datelist[0];
                let date = {} as { year: number, month: number };
                //后一年12月
                if (last.month + 1 > 12) {
                    date.month = 1
                    date.year = last.year + 1
                }
                else {
                    date.month = last.month + 1
                    date.year = last.year
                }
                datelist.unshift(date)
            }
        }
        return datelist.reverse();
    }

    checkMobile(number: string) {
        if (number) {
            var dianhuayanzhen = /^[1][0-9][0-9]{9}$/;
            if (dianhuayanzhen.test(number)) return true;
        }
        ElMessage.error('请输入合法的手机号')
        return false;
    }
    checkIdCardNumber(idnumber: string) {
        if (!idnumber) {
            ElMessage.error('请输入合法的手机号')
            return false;
        }
        var regIdCard =
            /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
        if (regIdCard.test(idnumber)) {
            // 准确性  
            if (idnumber.length == 18) {
                var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9,
                    10, 5, 8, 4, 2); // 将前17位加权因子保存在数组里  
                var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); // 这是除以11后，可能产生的11为余数，验证码，也保存成数组  
                var idCardWiSum = 0; // 用来保存前17为各自乘以加权因子后的总和  
                for (var i = 0; i < 17; i++) {
                    idCardWiSum += parseInt(idnumber.substring(i, i + 1)) * idCardWi[i];
                }
                var idCardMod = idCardWiSum % 11; // 计算出校验码所在数组的位置  
                var idCardlast = idnumber.substring(17); // 得到最后一位身份证号码  
                if (idCardMod == 2) {
                    if (idCardlast != "X" && idCardlast != "x") {
                        ElMessage.error('请输入合法的手机号')
                        return false
                    } else {
                        return true;
                    }
                } else {
                    if (idCardlast != idCardY[idCardMod] + '') {
                        ElMessage.error('请输入合法的手机号')
                        return false
                    } else {
                        return true
                    }
                }
            }
        } else {
            ElMessage.error('请输入合法的手机号')
            return false
        }
    }

}
const UtilsService = new UtilsServiceTemp();
export {
    UtilsService
}